/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.client.support;

/**
 * Utilities functions.
 */
public class GwtObjectUtils
{
  /**
   * Tests if 2 objects are both null, or equals.
   * 
   * @param object1
   *          the first object
   * @param object2
   *          the second object
   * @return <code>true</code> if either both object1 and object2 are
   *         <code>null</code> or if object1.equals(object2),
   *         <code>false</code> otherwise.
   */
  public static boolean nullSafeEquals(final Object object1, final Object object2)
  {
    if (object1 == null) {
      return (object2 == null);
    }
    return object1.equals(object2);
  }



  /**
   * returns an hashCode representing the {@link Object}. This method can take
   * <code>null</code> value and will then return 0.
   * 
   * @param obj
   *          to hashCode
   * @return hashCode representing obj
   */
  public static int nullSafeHashCode(final Object obj)
  {
    if (obj == null) {
      return 0;
    }

    return obj.hashCode();

  }



  /**
   * returns an hashCode representing the array of {@link Object}. This method
   * can take <code>null</code> value and will then return 0.
   * 
   * @param objs
   *          to hashCode
   * @return hashCode representing objs
   */
  public static int nullSafeHashCode(final Object[] objs)
  {
    if (objs == null) {
      return 0;
    }

    StringBuffer hashcode = new StringBuffer();

    for (int i = 0; i < objs.length; ++i) {

      if (objs[i] != null) {

        hashcode.append(objs[i].hashCode());

      }

    }

    return hashcode.toString().hashCode();

  }



  /**
   * private constructor.
   */
  private GwtObjectUtils()
  {
    // Do nothing.
  }
}
